package com.hddz.acquisition.view;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.Entity;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hddz.acquisition.R;
import com.hddz.acquisition.model.BaseListItem;
import com.hddz.acquisition.model.IListItem;
import com.hddz.acquisition.model.JsonRespose;
import com.hddz.acquisition.model.ProductOutItem;

public class ProductOutAdapter extends BaseAdapter {

    private LayoutInflater mInflater;
    //private List<ProductOutItem> mListItem;
    private List<JSONObject> mListItem;
    private Context mContext;
    private Boolean nodata = false;

    public ProductOutAdapter(Context context) {
        super();
        this.mContext = context;
        this.mInflater = LayoutInflater.from(context);
        //this.mListItem = new ArrayList<ProductOutItem>();
        this.mListItem = new ArrayList<JSONObject>();
    }

//	public void updateList(List<ProductOutItem> list) {
//		this.mListItem.clear();
//		this.mListItem.addAll(list);
//		notifyDataSetChanged();
//	}

    public void updateList(List<JSONObject> list) {
        this.mListItem.clear();
        this.mListItem.addAll(list);
        notifyDataSetChanged();
    }

//	public void removeItem(String cCode) {
//		BaseListItem deleteItem = null;
//		for (IListItem item : mListItem) {
//			if (((BaseListItem) item).cCode.equals(cCode)) {
//				deleteItem = (BaseListItem) item;
//				break;
//			}
//		}
//		if (deleteItem != null) {
//			this.mListItem.remove(deleteItem);
//			notifyDataSetChanged();
//		}
//	}

    public void removeItem(String cCode) {
        JSONObject deleteItem = null;
        for (JSONObject item : mListItem) {
            if (item.getString("cCode").equals(cCode)) {
                deleteItem = item;
                break;
            }
        }
        if (deleteItem != null) {
            this.mListItem.remove(deleteItem);
            notifyDataSetChanged();
        }
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mListItem.size();
    }

    @Override
    public JSONObject getItem(int position) {
        // TODO Auto-generated method stub
        return mListItem.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(
                    R.layout.view_stock_product_out_list_item, null);
            holder = new Holder();
//            holder.txt_code = (TextView) convertView
//                    .findViewById(R.id.txt_code);
//            holder.txt_cusName = (TextView) convertView
//                    .findViewById(R.id.txt_cusName);
//            holder.txt_invname_pm = (TextView) convertView
//                    .findViewById(R.id.txt_invname_pm);
//            holder.txt_horse = (TextView) convertView
//                    .findViewById(R.id.txt_horse);
//            holder.txt_quantity = (TextView) convertView
//                    .findViewById(R.id.txt_quantity);
            holder.tvInfo = (TextView) convertView
                    .findViewById(R.id.tv_info);

            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }

//        ProductOutItem item = mListItem.get(position);
//        holder.txt_code.setText(item.cCode);
//        holder.txt_cusName.setText(item.cCusName);
//        holder.txt_invname_pm.setText(item.cInvName);
//        holder.txt_horse.setText(item.iHourse + "");
//        holder.txt_quantity.setText(item.iQuantity + "");
        String html = "";
        JSONObject item = mListItem.get(position);
        html += "<font style='width:30px;'>单据：</font>" + item.getString("cCode") + "<br/>";
        for (Map.Entry<String, Object> e : item.entrySet()) {
            if (!(e.getValue() instanceof JSONArray) && e.getKey() != "cCode") {
                html += "<font style='width:30px;'>" + e.getKey() + "：</font>" + e.getValue() + "<br/>";
            }
        }
        JSONArray items = item.getJSONArray("Items");
        for (int i = 0; i < items.size(); i++) {
            if(i==0){
                html += "<font>--------------------------------------------------------</font>";
            }else {
                html += "<br/><font>--------------------------------------------------------</font>";
            }
            JSONObject obj = items.getJSONObject(i);
            for (Map.Entry<String, Object> o : obj.entrySet()) {
                html += "<br/><span style='width:30px;margin-left:10px;'> &nbsp;&nbsp;&nbsp;&nbsp;" + o.getKey() + "：</span>" + o.getValue() + "";
            }
        }

        holder.tvInfo.setText(Html.fromHtml(html));
        return convertView;
    }

    class Holder {
        //        private TextView txt_code, txt_cusName, txt_invname_pm, txt_horse,
//                txt_quantity;
        private TextView tvInfo;
    }
}
